//                            _ooOoo_
//                           o8888888o
//                           88" . "88
//                           (| -_- |)
//                            O\ = /O
//                        ____/`---'\____
//                       / .' \\| |// `. \
//                      / \\|||  :  |||// \
//                     / _||||| -:- |||||- \
//                     | | | \\\ - /// | |  |
//                     | \_| ''\---/'' | |  |
//                      \ .-\__ `-` ___/-. /
//                   ___`. .' /--.--\ `. . __
//                ."" '< `.___\_<|>_/___.' >'"".
//               | | : `- \`.;`\ _ /`;.`/ - ` : | |
//                 \ \ `-. \_ __\ /__ _/ .-` / /
//         ======`-.____`-.___\_____/___.-`____.-'======
//                            `=---='
//
//         .............................................
//                 佛祖镇楼                 BUG退让
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Park.Net.IService;
using Park.Net.Model;
using Park.Net.Utils;

namespace Park.Net.ParkWebAPI.Controllers
{
  

    // 查
    [ApiController]
    [Route("api/[controller]")]
    public class CarParkController : ControllerBase
    {
        
        private readonly IBaseServices<CarPark> _CarParkService;

        public CarParkController(IBaseServices<CarPark> CarParkService)
        {
            _CarParkService = CarParkService;
        }

        /*  [HttpGet("CarPark")]
         public async Task<IActionResult> GetCarPark(){
             var res = await _CarParkService.FindAllAsync();
             if(res!=null && res.Count>0){
                 return Ok(res);
             }
             return NotFound();
         } */
        [HttpGet("CarPark")]
        public async Task<ActionResult<ApiReturns>> GetCarPark()
        {
            var res = await _CarParkService.FindAllAsync();
            if (res != null && res.Count > 0)
            {
                return ApiReturnsHelper.Succeed(res);
            }
            return ApiReturnsHelper.Failed("查不到~");
        }


        //编辑

        // GET api/car/5
        [HttpGet("{id}")]
        public async Task<ActionResult<CarPark>> Get(int id)
        {
            var car = await _CarParkService.FindOneAsync(id);

            if (car == null)
            {
                return NotFound();
            }

            return car;
        }

        // PUT api/car/5
        [HttpPut("{id}")]
        public async Task<IActionResult> Put(int id, CarPark car)
        {
            var existingCar = await _CarParkService.FindOneAsync(id);


            if (existingCar == null)
            {
                return NotFound();
            }
                System.Console.WriteLine(existingCar);
           // copy fields from the modified car object to existing car object
   

            _CarParkService.UpdateAsync(car);
            // await _CarParkService.UpdateAsync(car);

            return Ok();
        }
    

    }
//删除
public class CarParkDelete : ControllerBase
{
    private readonly  IBaseServices<CarPark> _CarParkService;

    public CarParkDelete(IBaseServices<CarPark> CarParkService)
    {
        _CarParkService = CarParkService;
    }

    [HttpDelete("{id}")]
    public async Task<ActionResult> Delete(int id)
    {
        var vehicle = await _CarParkService.FindOneAsync(id);

        if (vehicle == null)
        {
            return NotFound();
        }

         _CarParkService.DeleteAsync(vehicle);
        await  _CarParkService.FindAllAsync();

        return NoContent();
    }
}
  // 添加
public class AddController : ControllerBase
{
    private readonly IBaseServices<CarPark> _context;

    public AddController(IBaseServices<CarPark> context)
    {
        _context = context;
    }

    // POST: api/Bike/AddBike
    [HttpPost]
    [Route("AddBike")]
    public async Task<ActionResult> AddBike(CarPark bike )
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        // Perform additional validation as needed
        if (string.IsNullOrEmpty(bike.Number) || string.IsNullOrEmpty(bike.Type))
        {
            return BadRequest("Bike number and type are required fields.");
        }

        // Add bike data to the database
       await _context.CreateAsync(bike);
      
        return Ok($"Bike with ID {bike.ID} was added successfully.");
    }
}
    
}